export default {
	data() {
		return {
			goods: {
				num: 1
			},
			payType: '', // 1微信  2积分
			jwt: '',
		};
	},
	async onLoad(options) {
		this.goodsId = options.goodsId ? options.goodsId : ''
		
		this.jwt = this.$store.get('jwt')
		var share = this.$store.get('share')
		
		if (!this.jwt && share == 1) {
			this.$store.set('goodsId', this.goodsId)
			this.$store.set('page', 'shop/detail/detail')
		}
		
		await this.getDetail()
	},
	onShareAppMessage(res) {
		return {
			title: this.goods.name,
			path: `pages/shop/detail/detail?goodsId=${this.goodsId}`,
			desc: "欢迎进入潮玩汇聚",
			content: "欢迎进入潮玩汇聚",
			imageUrl: this.goods.img,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	},
	methods: {
		// 获取详情
		async getDetail() {
			var res = await this.$get('server/index/getshopgoodsdetails', {
				id: this.goodsId
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.goods = res.goods
			this.onComputed()

			this.goods.specs.forEach(item => {
				item.active = ''
			})
			this.goods.num = 1
		},
		// 规格选择
		onSpecClick(cell, i, index) {
			this.goods.specs[index].active = cell.id
			this.onComputed()
		},
		// 计算
		onComputed() {
			var specsList = this.goods.specs
			var specsPrice = 0
			var specsScore = 0

			this.goods.specPrice = this.goods.price
			this.goods.specScore = this.goods.jifen


			var spec = ''

			for (var x = 0; x < specsList.length; x++) {
				var dataX = specsList[x]
				for (var y = 0; y < dataX.data.length; y++) {
					var dataY = dataX.data[y]
					if (dataX.active == dataY.id) {
						specsPrice += Number(dataY.price)
						specsScore += Number(dataY.jifen)
						spec += dataY.name + '/'
						var str = spec.substring(0, spec.length - 1)
						this.goods.specText = str
					}
				}
			}

			this.goods.specPrice = (Number(this.goods.price) + Number(specsPrice)).toFixed(2)
			this.goods.specScore = (Number(this.goods.jifen) + Number(specsScore)).toFixed(2)
			this.$forceUpdate()
		},
		// 数量加减
		onNumChange(type) {
			console.log('数量')
			if (type == 'jian') {
				if (this.goods.num <= 1) {
					return
				}
				this.goods.num--;
			} else {
				this.goods.num++;
			}
			this.$forceUpdate()
		},
		// 立即购买
		onBuy(type) {
			if (!this.jwt) {
				this.$toast('请先登录')
				setTimeout(() => {
					this.$reLaunchTo('pages/login/login')
				}, 1000)
				return
			}
			this.$refs.buy.open()
			this.payType = type
		},
		// 确认购买
		onConfirmBuy() {
			var specFlag = this.goods.specs.some(item => item.active == '')
			if (specFlag) {
				this.$toast('请选择规格')
				return
			}
			var list = []
			for (var i = 0; i < this.goods.specs.length; i++) {
				var item = this.goods.specs[i]
				list.push(item.active)
			}
			this.goods.specIdList = JSON.stringify(list)
			console.log('规格===')

			this.$navTo('pages/shop/submit/submit', {
				payType: this.payType,
				goods: encodeURIComponent(JSON.stringify(this.goods)),
			})
		},
		// 关闭购买弹窗
		onBuyClose() {
			this.$refs.buy.close()
			this.goods.num = 1
		},
	}
};
